﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Presentation.Pages
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                carregarNoticias();
        }

        private void carregarNoticias()
        {
            try
            {
                using (ministerioprosperid1Entities bd = new ministerioprosperid1Entities())
                {
                    List<DENoticia> noticias = bd.DENoticias.Where(q => q.Ativo == true).OrderByDescending(q => q.Data).ToList();
                    if (noticias != null)
                    {
                        StringBuilder sb = new StringBuilder();
                        bool right = true;

                        foreach (DENoticia item in noticias)
                        {
                            sb.AppendLine("<div class=\"news\">");

                            sb.AppendLine("<h1 class=\"title\">");
                            sb.AppendLine(item.Titulo);
                            sb.AppendLine("</h1>");

                            sb.AppendLine("<p class=\"meta\">");
                            sb.AppendLine("<small>");
                            sb.Append(item.Data.Day.ToString().PadLeft(2,'0'));
                            sb.Append(" de ");
                            sb.Append(Util.buscarMesPorExtenso(item.Data.Month));
                            sb.Append(" de ");
                            sb.Append(item.Data.Year);
                            sb.AppendLine("</small>");
                            sb.AppendLine("</p>");

                            foreach (DENoticiaFoto foto in item.TBNoticiaFotoes)
                            {
                                sb.Append("<img src=\"");
                                sb.Append(foto.Caminho);
                                sb.Append("\" alt=\"");
                                sb.Append(foto.Alt);
                                sb.Append("\" height=\"190px\" width=\"230px\" onclick=\"abrir('");
                                sb.Append(foto.Caminho);
                                sb.Append("')\" style=\"float: ");

                                if (right)
                                    sb.Append("right");
                                else
                                    sb.Append("left");

                                right = !right;

                                sb.AppendLine("; margin: 5px; cursor: pointer\" />");
                            }

                            sb.AppendLine(item.CorpoHTML);
                            sb.AppendLine("<br />");
                            sb.AppendLine("<br />");

                            if (!String.IsNullOrEmpty(item.Versiculo))
                            {
                                sb.AppendLine(item.Versiculo);
                                sb.AppendLine("<br />");
                                sb.AppendLine("<br />");
                            }

                            sb.AppendLine("</div>");
                        }

                        if (!String.IsNullOrEmpty(sb.ToString()))
                            ltlNoticias.Text = sb.ToString();
                    }
                }
            }
            catch (Exception)
            {
                Util.showMessage(this, "Houve um problema ao carregar as notícias");
            }
        }
    }
}